package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.QuartzTask;
import cn.stylefeng.guns.sys.modular.system.model.params.QuartzTaskParam;
import cn.stylefeng.guns.sys.modular.system.model.params.TaskParam;
import cn.stylefeng.guns.sys.modular.system.quartz.enums.JobStatusEnum;
import cn.stylefeng.guns.sys.modular.system.service.QuartzTaskService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * job控制器
 *
 * @author jiangge
 */
@Controller
@RequestMapping("/quartz")
public class QuartzTaskController extends BaseController {

    private String PREFIX = "/modular/system/quartz/";

    @Autowired
    private QuartzTaskService taskService;

    /**
     * 进入任务管理界面
     *
     * @return
     */
    @RequestMapping("")
    public String taskScheduleJob() {
        return PREFIX + "task.html";
    }

    @RequestMapping("/add")
    public String taskAddScheduleJob() {
        return PREFIX + "task_add.html";
    }

    /**
     * 查看详情接口
     *
     * @author jiangge
     * @Date 2020-09-10
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(TaskParam taskParam) {
        QuartzTask detail = this.taskService.getById(taskParam.getId());
        return ResponseData.success(detail);
    }

    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(QuartzTaskParam query) {
        // 查询列表数据
        LayuiPageInfo result = taskService.findPageBySpec(query);
        return result;
    }
    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-09-10
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "task_edit.html";
    }


    /**
     * 修改任务
     *
     * @param task
     * @return
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData edit(QuartzTask task) {
        QuartzTask taskServer = taskService.get(task.getId());
        if (JobStatusEnum.RUNNING.getCode().equals(taskServer.getJobStatus())) {
            return new ErrorResponseData(403, "修改之前请先停止任务！");
        }
        taskService.update(task);
        return new SuccessResponseData();
    }

    /**
     * 修改任务状态
     *
     * @param id
     * @param jobStatus
     * @return
     */
    @RequestMapping("/changeStatus")
    @ResponseBody
    public ResponseData changeStatus(@RequestParam Long id, Boolean jobStatus) {
        //true:运行，false:停止
        String status = jobStatus == true ? JobStatusEnum.RUNNING.getCode() : JobStatusEnum.STOP.getCode();
        try {
            taskService.changeStatus(id, status);
            return new SuccessResponseData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ErrorResponseData(403, "任务状态修改失败");
    }

    /**
     * 删除任务
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData remove(QuartzTaskParam param) {
        QuartzTask taskServer = taskService.get(param.getId());
        if (JobStatusEnum.RUNNING.getCode().equals(taskServer.getJobStatus())) {
            return new ErrorResponseData("删除失败,删除前请先停止任务！");
        }
        if (taskService.remove(param.getId()) > 0) {
            return new SuccessResponseData();
        }
        return new ErrorResponseData("删除失败！");
    }

    /**
     * 立即运行
     */
    @RequestMapping("/runtask")
    @ResponseBody
    public ResponseData run(QuartzTaskParam param) {
        QuartzTask taskServer = taskService.get(param.getId());
        try {
            if (JobStatusEnum.STOP.getCode().equals(taskServer.getJobStatus())) {
                return new ErrorResponseData("执行失败!要执行任务,请先开启任务！");
            }
            taskService.run(taskServer);
            return new SuccessResponseData();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return new ErrorResponseData("执行任务失败！");
    }

    /**
     * 删除 批量删除任务
     */

    @PostMapping("/removeBatch")
    @ResponseBody
    public ResponseData removeBatch(@RequestParam("ids[]") Long[] ids) {
        for (Long id : ids) {
            QuartzTask taskServer = taskService.get(id);
            if (JobStatusEnum.RUNNING.getCode().equals(taskServer.getJobStatus())) {
                return new ErrorResponseData(403, "删除前请先停止任务！");
            }
        }
        taskService.removeBatch(ids);
        return new SuccessResponseData();
    }

    /**
     * 新增接口
     */
    @ResponseBody
    @PostMapping("/addItem")
    public ResponseData save(QuartzTaskParam task) {
        taskService.add(task);
        return ResponseData.success();
    }
}
